package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.BusinessRuntimeException;
import com.itheima.health.dao.MenuDao;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Service
@Slf4j
public class MenuServiceImpl implements MenuService {
    @Autowired
    private MenuDao menuDao;
    @Override
    public PageResult pageQuery(QueryPageBean queryPageBean) {
        log.info("[菜单-分页查询]data：{}",queryPageBean);
        //使用分页插件
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        // 分页查询数据库
        Page<Menu> page = menuDao.selectByConditon(queryPageBean.getQueryString());
        //封装分页数据
        return new PageResult(page.getTotal(),page.getResult());
    }
    @Transactional
    @Override
    public void add(Menu menu) {
        log.info("[菜单-添加]data：{}",menu);
        menuDao.insert(menu);
    }
    @Transactional
    @Override
    public void delete(Integer id) {
        log.info("[菜单-根据id删除]id：{}",id);
        menuDao.deleteById(id);
    }
    @Transactional
    @Override
    public void edit(Menu menu) {
        log.info("[菜单-编辑]data:{}",menu);
        menuDao.update(menu);
    }
    @Override
    public Menu findById(Integer id) {
        log.info("[菜单-根据ID查询]id:{}",id);
        return menuDao.selectById(id);
    }

    @Override
    public List<Menu> findAll() {
        log.info("查询所有菜单");
        return menuDao.findAll();
    }

    @Override
    public List<Menu> getMenuByUsername(String username) {
        List<Menu> menuList = menuDao.findMenuByUsername(username);
        for (Menu menu : menuList) {
            List<Menu> getSecondMenuByUsernameAndId = menuDao.getSecondMenByUsernameAndId(menu.getId(), username);
            menu.setChildren(getSecondMenuByUsernameAndId);
        }
        return menuList;
    }
}
